/**
 * 时间格式转换
 * @param strTime 要转换的时间戳
 */
 const formatDate = (strTime) => {
  let date = strTime ? new Date(strTime) : new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return year + '/' + month + '/' + day
}
/**
 * 星期
 * @param strTime 要转换的时间戳
 */
const getDay = (strTime) => {
  let date = strTime ? new Date(strTime) : new Date()
  let day = date.getDay()
  const weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
  return weekday[day]
}
// 设置存储
const setStore = (name, content) => {
  if (!name) return
  if (typeof content !== 'string') {
    content = JSON.stringify(content)
  }
  window.localStorage.setItem(name, content)
}
// 获取存取
const getStore = (name) => {
  if (!name) return
  return window.localStorage.getItem(name)
}

var lastResult
const handleDataCon = (arr) => {
  let alarmTime = getStore('alarmTime')
  let lockTime = getStore('lockTime')
  let connStatus = 0 // 0为离线 1为在线
  var conTimes, alarmTimes, alarmObj, lockObj
  let result = ''
  if (Number(arr) === 0) {
    result = { name: 'kong'}
    return result
  }
  
  let len = arr && arr.length
  // 门锁在线的时候 报警时间 大于等于门锁在线时间
  arr.forEach(function (item,index){
    // debugger
    if (item.name === 'connectionStatus' && item.value === '1') {
      conTimes = item.lastUpdateTimeMs
      return
    }
    if (item.name === 'dh.alarmType') {
      alarmTimes = item.lastUpdateTimeMs
      alarmObj = item
      return
    }
    if (item.name === 'unlockWay') {
      lockObj = item
      return
    }
  })
  if ( alarmTimes >= conTimes) {
    result = alarmObj
  } else if (alarmTimes < conTimes) {
    result = lockObj
  }
  return result
}

/**
 * template 注册方法
 */
const lockWay = 'unlockWay' // 开门方式
const alarmType = 'dh.alarmType' // 报警类型
// 时间格式化
template.helper('formatDate', function (onedate = true, strTime) {
  let date = strTime ? new Date(strTime) : new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  let result = ''
  if (onedate) {
    result = year + '/' + month + '/' + day
  } else {
    result = hours + ':' + minutes + ':' + seconds
  }
  return result
})

// 过滤器 参数名 和参数值对应的实际文字
  template.helper('filterParamName', function(key, value){
    let result = ''
    let PValue = parseInt(value)
    const lock = ['密码', '指纹', '射频卡']
    const alarm = ['非法用户（非法尝试开门）', '防拆报警', '低电压报警']
    const connect = ['连接','断开']
    // 传入的值为 功能名 例如 unlockWay 对应的值 0,1,2 unlockWay为功能名, 0,1,2为功能值
    if (key === 'unlockWay') {
      result = '开锁方式' + lock[PValue]
    } else if (key === 'dh.alarmType') {
      result = '报警类型' + alarm[PValue]
    } else if (key === 'connectionStatus') {
      result = '门锁' + connect[PValue]
    }
    return result;
  })

// 加载图标
let loaderEle = document.getElementById('loader');
function startLoading() {
    loaderEle.setAttribute('class', '');
}
function stopLoading() {
    loaderEle.setAttribute('class', 'hide');
}

// 提示 toast
let toastEle = document.querySelector('#toast')
function showToast (text) {
  let  toast = document.getElementsByClassName('toast-hook')[0]
  toastEle.setAttribute('class', '');
  toast.innerHTML = text
  setTimeout(function () {
    hideToast()
  }, 2000)
}
function hideToast () {
  toastEle.setAttribute('class', 'hide');
}
